package com.horoscope.astrology.zodiac.signs.free

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.horoscope.astrology.zodiac.signs.free.bean.BaseBean
import com.horoscope.astrology.zodiac.signs.free.bean.ConfigResultBean
import com.horoscope.astrology.zodiac.signs.free.bean.QuoteBean
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVLogLevel

object AppSettingManager {

    private const val USER_PREFERENCES_FILE = "config"
    val context = MmoApplication.getInstance()

    //    val rootDir = MMKV.initialize(context)
    val mmkv: MMKV by lazy {
        MMKV.initialize(context)
        MMKV.setLogLevel(MMKVLogLevel.LevelDebug)
        MMKV.mmkvWithID(USER_PREFERENCES_FILE)
    }

    fun clear() {
        mmkv.clearAll()
    }

    fun contains(key: String): Boolean {
        return mmkv.contains(key)
    }

    fun <T> saveList(dataList: List<T>, key: String) {
        val gSon = Gson()
        val strJson = gSon.toJson(dataList)
        mmkv.encode(key, strJson)
    }

    fun <T> readList(key: String): List<T>? {
        val strJson = mmkv.decodeString(key, null) ?: return null
        val gSon = Gson()
        return gSon.fromJson(strJson, object : TypeToken<List<T>>() {}.type)
    }

    // 用户信息会经常修改，要保持 实时 同步性，所以用commit
    fun <T : BaseBean> saveObject(bean: T, key: String) {
        val gSon = Gson()
        val strJson = gSon.toJson(bean)
        mmkv.encode(key, strJson)
    }

    // 启动读取用户数据
    fun readObject(key: String): UserBean? {
        val strJson = mmkv.decodeString(key, null) ?: return null
        val gSon = Gson()
        return gSon.fromJson(strJson, object : TypeToken<UserBean>() {}.type)
    }

    fun readQuote(key: String): QuoteBean? {
        val strJson = mmkv.decodeString(key, null) ?: return null
        val gSon = Gson()
        return gSon.fromJson(strJson, object : TypeToken<QuoteBean>() {}.type)
    }

    fun saveString(value: String?, key: String) {
        if (value == null) {
            mmkv.remove(key)
        } else {
            mmkv.encode(key, value)
        }
    }

    fun readString(key: String, defValue: String): String {
        return mmkv.decodeString(key, defValue)
    }

    fun saveInt(value: Int, key: String) {
        mmkv.encode(key, value)
    }

    fun readInt(key: String, defValue: Int): Int {
        return mmkv.decodeInt(key, defValue)
    }

    fun saveLong(value: Long, key: String) {
        mmkv.encode(key, value)
    }

    fun readLong(key: String, defValue: Long): Long {
        return mmkv.decodeLong(key, defValue)
    }

    fun saveFloat(value: Float, key: String) {
        mmkv.encode(key, value)
    }

    fun readFloat(key: String, defValue: Float): Float {
        return mmkv.decodeFloat(key, defValue)
    }

    // 主要用于分辨是否看完了激励广告
    fun saveBoolean(value: Boolean, key: String) {
        mmkv.encode(key, value)
    }

    fun readBoolean(key: String, defValue: Boolean): Boolean {
        return mmkv.decodeBool(key, defValue)
    }

    fun readAdConfigObject(key: String): ConfigResultBean.ConfigResultBeanData? {
        val strJson = mmkv.decodeString(key, null) ?: return null
        val gSon = Gson()
        return gSon.fromJson(
            strJson,
            object : TypeToken<ConfigResultBean.ConfigResultBeanData>() {}.type
        )
    }

    // 指定天数打点（只打一次）
    fun recordFirstTime(key: String): Boolean {
        val isFirstCom = mmkv.decodeBool(key, true);

        return if (!isFirstCom) {
            false
        } else {
            mmkv.encode(key, false)
            true
        }
    }

    // 应用启动次数
    fun appOpenCount(): Int {
        var count = mmkv.decodeInt("app_open", 0)
        mmkv.encode("app_open", ++count)

        return count
    }

}